//
//  LBMonoChrome.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 11/27/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

#include <iostream>
#include "LBMonoChromeFilter.h"
#include "LBGraphics.h"

char monoChromeFragShaderStr[] = ""

"precision mediump float;"

"varying highp vec2 vtexCoor;"

"uniform sampler2D texture;"
"uniform float intensity;"
"uniform vec3 filterColor;"

"const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);"

"void main()"
"{"
	//desat, then apply overlay blend
"	lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"	float luminance = dot(textureColor.rgb, luminanceWeighting);"
	
"	lowp vec4 desat = vec4(vec3(luminance), 1.0);"
	
	//overlay
"	lowp vec4 outputColor = vec4("
"                                (desat.r < 0.5 ? (2.0 * desat.r * filterColor.r) : (1.0 - 2.0 * (1.0 - desat.r) * (1.0 - filterColor.r))),"
"                                 (desat.g < 0.5 ? (2.0 * desat.g * filterColor.g) : (1.0 - 2.0 * (1.0 - desat.g) * (1.0 - filterColor.g))),"
"                                 (desat.b < 0.5 ? (2.0 * desat.b * filterColor.b) : (1.0 - 2.0 * (1.0 - desat.b) * (1.0 - filterColor.b))),"
"                                 1.0"
"                                 );"
	
	//which is better, or are they equal?
"	gl_FragColor = vec4( mix(textureColor.rgb, outputColor.rgb, intensity), textureColor.a);"
"}";

LBMonoChromeFilter::LBMonoChromeFilter(){
    init(0, monoChromeFragShaderStr);
    
    intensityUniform = program->uniformIndex("intensity");
    filterColorUniform = program->uniformIndex("filterColor");
    
    setIntensity(1.0);
    setFilterColor(0.1, 0.7, 0.1);
    
}

void LBMonoChromeFilter::setIntensity(float intensity){
    this->intensity = intensity;
    LBGraphics::getInstance()->useProgram(program);
    glUniform1f(intensityUniform, intensity);
}

void LBMonoChromeFilter::setFilterColor(float red, float green, float blue){
    filterColor.one = red;
    filterColor.two = green;
    filterColor.three = blue;
    LBGraphics::getInstance()->useProgram(program);
    glUniform3f(filterColorUniform, red, green, blue);
}